#!/usr/bin/env python


"""
Extract the full list of emoji and names from the Unicode Consortium and
apply as much formatting as possible so the codes can be dropped into the
emoji registry file.

Written and run with Python3.  Not tested on Python2 and definitely not
intended for production use.

http://www.unicode.org/Public/emoji/1.0/full-emoji-list.html
"""


import requests
from bs4 import BeautifulSoup
from collections import OrderedDict


url = 'http://www.unicode.org/Public/emoji/1.0/full-emoji-list.html'


response = requests.get(url)
print response.content
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")

# with open('utils/content.html') as f:
#     soup = BeautifulSoup(f.read())

header = [
    'Count', 'Code', 'Browser', 'B&W*', 'Apple', 'Andr', 'One', 'Twit', 'Wind', 'GMail',
    'DCM', 'KDDI', 'SB', 'Name', 'Version', 'Default', 'Annotations'
]
header = ['No',	'Code',	'Brow.', 'Chart', 'Apple', 'Twtr.',	'One',
          'Googᵈ', 'Sams.', 'Wind.', 'GMail', 'Sb', 'Dcm',
          'Kddi', 'Name', 'Year', 'Default', 'Annotations']
output = {}

i = 0
j = 0
for row in soup.find('table').find_all('tr'):
    cols = row.find_all('td')
    cols = [e.text for e in cols]
    print 'cols:', len(cols)
    d = OrderedDict(zip(header, [e for e in cols]))

    if d:
        _code = []
        for c in d['Code'].split(' '):
            if len(c) is 6:
                _code.append(c.replace('+', '0000'))
            else:
                _code.append(c.replace('+', '000'))
        code = ' '.join(_code)
        name = d['Name'].replace(' ', '_').strip()
        char = "u'" + code.replace('U', '\\U') + "',"
        output.update({name: char})

for name in sorted(output.keys()):
    print("    '%s': %s" % (name, output[name]))
